package org.dept.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dept.mapper.UserMapper;
import org.dept.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserMapper userMapper;
    //查询
    @GetMapping("/list")
    public List<User> list(){
        List<User> users = userMapper.selectList(null);
        return users;
    }
    //新增
    @GetMapping("/add")
    public int add(){
        User user=new User();
        user.setName("zzh");
        user.setAge(3);
        user.setEmail("1321564");
        int result = userMapper.insert(user);
        System.out.println(result);
        System.out.println("--->"+user);
        return result;
    }
    //修改
    @GetMapping("/update/{id}")
    public int update(@PathVariable("id")int id){
        User user=new User();
        user.setId(id);
        user.setName("zzh");
        user.setAge(4);
        user.setEmail("123");
        int result = userMapper.updateById(user);
        System.out.println(result);
        System.out.println("--->"+user);
        return result;
    }
    //乐观锁成功
    @GetMapping("/ok")
    public String ok(){
        User user = userMapper.selectById(8);
        user.setName("zzh");
        user.setEmail("99999");
        userMapper.updateById(user);
        System.out.println("成功");
        return "ok";
    }
    //乐观锁失败
    @GetMapping("/ok2")
    public String error(){
        //线程1
        User user = userMapper.selectById(8);
        user.setName("zzh111");
        user.setEmail("99999");
        //线程2
        User user2 = userMapper.selectById(8);
        user2.setName("zzh222");
        user2.setEmail("99999");
        //插队，导致乐观锁version不一致
        userMapper.updateById(user2);

        //因为被插队可以使用自旋锁来多次尝试提交！
        //没有乐观锁就会覆盖插队线程的值
        userMapper.updateById(user);
        System.out.println("成功");
        return "ok2";
    }

    //测试单个查询用户
    @GetMapping("/get/{id}")
    public User selectById(@PathVariable("id")int id){
        User user = userMapper.selectById(id);
        System.out.println(user);
        return user;
    }

    //测试查询多个用户
    @GetMapping("/users")
    public List<User> selectByBatchId(){
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        return users;
    }

    //条件查询map
    @GetMapping("/map")
    public List<User> mapSelect(){
        HashMap<String,Object> map =new HashMap<String,Object>();
        //自定义查询
        map.put("name","zzh");
        map.put("age",4);
        List<User> users = userMapper.selectByMap(map);
        return users;
    }

    //测试分页查询
    @GetMapping("/page")
    public List<User> pageSelect(){
        Page<User> page = new Page<User>(1,5);
        userMapper.selectPage(page,null);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    //如果没有用逻辑删除，数据会直接删除，
    //使用逻辑删除，数据不会删除，而是修改
    //测试单个删除
    @GetMapping("/del/{id}")
    public int delete(@PathVariable("id")int id){
        return userMapper.deleteById(id);
    }
    //测试多个删除
    @GetMapping("/deletes")
    public int deletes(){
        return  userMapper.deleteBatchIds(Arrays.asList(6,7));
    }
    //通过map删除
    @GetMapping("/delMap")
    public int delMap(String name){
        HashMap<String,Object> map=new HashMap();
        map.put("name",name);
        return userMapper.deleteByMap(map);
    }

}
